package com.newtouch.bxzs.business.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.business.enclosure.mapper.AttachmentMapper;
import com.newtouch.bxzs.business.message.service.AnnouncementService;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.model.Attachment;
import com.newtouch.bxzs.common.base.model.bo.PageParam;
import com.newtouch.bxzs.common.base.util.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.enclosure.service.AttachmentService;
import com.newtouch.bxzs.business.product.controller.ProductController;
import com.newtouch.bxzs.business.product.mapper.ProductMapper;
import com.newtouch.bxzs.business.product.mapper.UserMapper;
import com.newtouch.bxzs.business.product.model.*;
import com.newtouch.bxzs.business.product.service.ProductService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.ButtonTypeEnum;
import com.newtouch.bxzs.common.base.em.ProductStatus;
import com.newtouch.bxzs.common.base.em.PublishStatusEnum;
import com.newtouch.bxzs.common.base.model.Product;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.model.vo.EnclosureVO;
import com.newtouch.bxzs.common.base.util.CheckPublishDateUtils;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 产品实现类
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020/12/31 15:47</td>
 * <td>xiongdianqiang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author xiongdianqiang
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Autowired
    private AttachmentService attachmentService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 静态变量：系统日志
     */
    private static final Log logger = LogFactory.getLog(ProductController.class);

    /**
     * getProductListByMarketing
     * 获取配置的产品的发布时间的最大限制
     */
    @Value("${publishTimeConfig.maxPublishTime}")
    private Integer maxPublishTime;

    /**
     * 获取配置的产品的撤销时间的最大限制
     */
    @Value("${revokeTimeConfig.maxRevokeTime}")
    private String maxRevokeTime;

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#getProductList(
     * com.newtouch.bxzs.business.product.model.ProductVO)
     */
    @Override
    public IPage<ProductBO> getProductList(ProductVO productVO, Boolean way) {
        /* 校验 */
        if (productVO == null || productVO.getPageSize() == null || productVO.getCurrentPage() == null
                || StringUtils.isEmpty(productVO.getPageSize()) || StringUtils.isEmpty(productVO.getCurrentPage())) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        if (!StringUtils.isEmpty(productVO.getPublishStatus())) {
            boolean condition = !productVO.getPublishStatus().equals(PublishStatusEnum.UNPUBLISH_STATUS.getResultCode())
                    && !productVO.getPublishStatus().equals(PublishStatusEnum.PUBLISHED_STATUS.getResultCode())
                    && !productVO.getPublishStatus().equals(PublishStatusEnum.REVOKED_STATUS.getResultCode());
            if (condition) {
                logger.info("查询列表失败");
                throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR);
            }
        }
        /* 实例化一个QueryWrapper */
        QueryWrapper<Product> query = new QueryWrapper<>();
        /* 判断查看的是产品列表还是发布时查看的未发布产品列表，true是前者，false是后者 */
        if (!way) {
            /* 只能查看同步过来后未发布的产品（修改时间为空）或者处于删除状态的产品 */
            query.isNull("UPDATE_TIME").or().eq("IS_DELETE", 1);
            /* 设置状态为未发布状态 */
            productVO.setPublishStatus(PublishStatusEnum.UNPUBLISH_STATUS.getResultCode());
        } else {
            /* 删除状态不能查询 */
            query.eq("IS_DELETE", 0);
            /* 只能查看同步过来后已发布的产品(修改时间不为空) */
            query.isNotNull("UPDATE_TIME");
        }
        /* 只查看在售状态的产品 */
        query.eq("PRODUCT_STATUS", ProductStatus.ON_SALE.getCode());
        /* 判断是否传入产品名称查询条件 */
        if (!StringUtils.isEmpty(productVO.getProductName())) {
            query.like("PRODUCT_NAME", productVO.getProductName());
        }
        /* PC端展示产品信息(产品列表、选择产品列表),按照创建时间降序排列 */
        query.orderByDesc("CREATE_TIME");
        /* 判断是否传入产品状态查询条件(PC端) */
        if (productVO.getPublishStatus() != null && !StringUtils.isEmpty(productVO.getPublishStatus())) {
            query.eq("PUBLISH_STATUS", productVO.getPublishStatus());
        }
        Page<Product> p = new Page<>(productVO.getCurrentPage(), productVO.getPageSize());
        IPage<Product> productPage = productMapper.selectPage(p, query);
        if (productPage == null) {
            return null;
        }
        IPage<ProductBO> page = new Page<>();
        /* 将转换的BO放到advertisementBOList的Records */
        page.setRecords(EntityToBeanUtils.copyBean(productPage.getRecords(), ProductBO.class));
        page.setCurrent(productPage.getCurrent());
        page.setPages(productPage.getPages());
        page.setSize(productPage.getSize());
        page.setTotal(productPage.getTotal());
        return page;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductDetail(java.lang.String)
     */
    @Override
    public ProductDetailBO getProductDetail(String productId) {
        /* 校验 */
        if (StringUtils.isEmpty(productId)) {
            logger.info("查看详情失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return null;
        }
        ProductDetailBO productDetailBO = new ProductDetailBO();
        /* 使用工具直接进行实体与BO的复制转换，注意字段名及类型需保持一致 */
        BeanUtil.copyProperties(product, productDetailBO);
        /* 获取附件 */
        HashMap<String, List<String>> result = attachmentService.getEnclosureList(new EnclosureVO(productId, null));
        if (result != null) {
            productDetailBO.setProductEnclosureNameList(result);
        }
        /* 获取发布人 */
        User user = userMapper.selectById(product.getUpdateUserId());
        if (!BeanUtil.isEmpty(user)) {
            productDetailBO.setPublishUser(user.getUserName());
        }
        return productDetailBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#deleteProduct(
     * java.util.List)
     */
    @Override
    public Integer deleteProduct(List<String> productIds) {
        /* 校验 */
        if (CollectionUtil.isEmpty(productIds)) {
            logger.info("删除失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        QueryWrapper<Product> query = new QueryWrapper<>();
        query.in("PRODUCT_ID", productIds).eq("PUBLISH_STATUS", PublishStatusEnum.PUBLISHED_STATUS.getResultCode());
        List<Product> products = productMapper.selectList(query);
        /* 判断集合内是否有发布状态的产品 */
        if (!CollectionUtil.isEmpty(products)) {
            logger.info("删除失败");
            throw new BaseException(BaseErrorEnum.PUBLISH_STATUS);
        }
        /* 设置修改人状态和修改时间 */
        QueryWrapper<Product> updateQuery = new QueryWrapper<>();
        updateQuery.in("PRODUCT_ID", productIds);
        Product product = new Product();
        product.setUpdateTime(new Date());
        /* 将删除的产品设置为未发布状态 */
        product.setPublishStatus(PublishStatusEnum.UNPUBLISH_STATUS.getResultCode());
        product.setUpdateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());

        product.setDeleteFlag(true);
        /* 批量逻辑删除产品 */
        if (productMapper.update(product, updateQuery) > 0) {
            /* 批量删除附件信息 */
            attachmentMapper.delete(new QueryWrapper<Attachment>().in("ATTACHMENT_RELATION_ID", productIds));
        }
        return null;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * changeProductState(com.newtouch.bxzs.business.product.model.
     * ChangeStateVO)
     */
    @Override
    public Integer changeProductState(ChangeStateVO changeStateVO) {
        /* 校验 */
        if (changeStateVO == null) {
            logger.info("修改状态失败");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        boolean condition = StringUtils.isEmpty(changeStateVO.getProductId())
                || StringUtils.isEmpty(changeStateVO.getButtonType())
                || !Objects.equals(changeStateVO.getButtonType(), ButtonTypeEnum.PUBLIC_BUTTON.getCode())
                && !Objects.equals(changeStateVO.getButtonType(), ButtonTypeEnum.REVOKE_BUTTON.getCode());
        if (condition) {
            logger.info("修改状态失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        Product product = new Product();
        Product product1 = productMapper.selectById(changeStateVO.getProductId());
        if (BeanUtil.isEmpty(product1)) {
            logger.info("id不正确");
            throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        if (Objects.equals(changeStateVO.getButtonType(), ButtonTypeEnum.PUBLIC_BUTTON.getCode())) {
            /* 点击的发布按钮 */
            if (productMapper.selectById(changeStateVO.getProductId()).getPublishStatus()
                    .equals(PublishStatusEnum.PUBLISHED_STATUS.getResultCode())) {
                logger.info("修改状态失败");
                throw new BaseException(BaseErrorEnum.PUBLISH_STATUS);
            }
            /* 判断撤销时间是否大于当前时间，若大于，则不执行操作 */
            if (product1.getRevokeTime() != null) {
                if (product1.getRevokeTime().after(new Date())) {
                    product.setRevokeTime(product1.getRevokeTime());
                }
            }
            product.setPublishStatus(PublishStatusEnum.PUBLISHED_STATUS.getResultCode());
            product.setPublishTime(new Date());
        } else {
            /* 点击的撤销按钮 */
            product.setPublishTime(product1.getPublishTime());
            product.setPublishStatus(PublishStatusEnum.REVOKED_STATUS.getResultCode());
            product.setRevokeTime(new Date());
        }
        /* 设置修改时间和修改人 */
        product.setUpdateTime(new Date());
        product.setUpdateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
        product.setDeleteFlag(false);
        QueryWrapper<Product> query = new QueryWrapper<>();
        query.eq("PRODUCT_ID", changeStateVO.getProductId());
        return productMapper.update(product, query);
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#modifyProduct(
     * com.newtouch.bxzs.business.product.model.ModifyProductVO)
     */
    @Override
    public String modifyProduct(ModifyProductVO modifyProductVO) {
        /* 判断传入数据是否合格 */
        if (modifyProductVO == null) {
            logger.info("新增或修改失败");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        boolean condition = !modifyProductVO.getButtonType().equals(ButtonTypeEnum.TEMPORARY_BUTTON.getCode())
                && !modifyProductVO.getButtonType().equals(ButtonTypeEnum.PUBLIC_BUTTON.getCode())
                || StringUtils.isEmpty(modifyProductVO.getProductId());
        if (condition) {
            logger.info("新增或修改失败");
            throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        /* 效验发布时间和撤销时间是否正确 */
        CheckPublishDateUtils.checkDate(modifyProductVO.getPublishTime(), modifyProductVO.getRevokeTime(),
                maxPublishTime, maxRevokeTime);
        Product product = new Product();
        /* 使用工具直接进行实体与BO的复制转换，注意字段名及类型需保持一致 */
        BeanUtil.copyProperties(modifyProductVO, product);
        /* 默认为暂存，保存为未发布状态 */
        product.setPublishStatus(PublishStatusEnum.UNPUBLISH_STATUS.getResultCode());
        /* 根据vo传入的按钮类型，判断是发布还是暂存（buttonType（1：暂存，2：发布，3：撤销）） */
        if (Objects.equals(modifyProductVO.getButtonType(), ButtonTypeEnum.PUBLIC_BUTTON.getCode())) {
            /* 点击发布按钮 */
            product.setPublishTime(new Date());
            /* 设置产品状态是已发布状态 */
            product.setPublishStatus(PublishStatusEnum.PUBLISHED_STATUS.getResultCode());
        }
        QueryWrapper<Product> query = new QueryWrapper<>();
        query.eq("PRODUCT_ID", modifyProductVO.getProductId());
        if (Objects.equals(productMapper.selectById(modifyProductVO.getProductId()).getPublishStatus(),
                PublishStatusEnum.UNPUBLISH_STATUS.getResultCode())) {
            /* 新增产品发布 */
            logger.info("新增产品发布");
            /* 设置创建人id */
            product.setCreateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
            /* 设置创建时间 */
            product.setCreateTime(new Date());
            /* 设置修改人id */
            product.setUpdateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail().getUserId()));
            /* 设置修改时间 */
            product.setUpdateTime(new Date());
            /* TODO 设置产品链接，地址未定，后续上线需要改动 */
            product.setProductUrl("product/detail?id=" + modifyProductVO.getProductId());
        } else {
            /* 修改 */
            /* 若是发布状态或者id为空 */
            if (StringUtils.isEmpty(modifyProductVO.getProductId())
                    || Objects.equals(productMapper.selectById(modifyProductVO.getProductId()).getPublishStatus(),
                    PublishStatusEnum.PUBLISHED_STATUS.getResultCode())) {
                logger.info("修改失败");
                throw new BaseException(BaseErrorEnum.PUBLISH_STATUS);
            }
            logger.info("修改产品");
            /* 设置修改人id */
            product.setUpdateUserId(
                    Objects.requireNonNull(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId()));
            /* 设置修改时间 */
            product.setUpdateTime(new Date());
        }
        product.setDeleteFlag(false);
        productMapper.update(product, query);
        return modifyProductVO.getProductId();
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductMarketingDiscourseList(com.newtouch.bxzs.business.product.model
     * .ProductAppVO)
     */
    @Override
    public IPage<ProductMarketingDiscourseBO> getProductMarketingDiscourseList(ProductAppVO productAppVO) {
        /* 校验 */
        if (productAppVO == null || productAppVO.getCurrentPage() == null || productAppVO.getPageSize() == null) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        /* 查询数据 */
        Page<ProductMarketingDiscourseBO> page = new Page<>(productAppVO.getCurrentPage(), productAppVO.getPageSize());
        return productMapper.getProductMarketingDiscourseList(page, productAppVO);
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductMarketingDiscourseDetail(java.lang.String)
     */
    @Override
    public ProductMarketingDiscourseDetailBO getProductMarketingDiscourseDetail(String productId) {
        /* 校验 */
        if (!productMapper.selectById(productId).getPublishStatus()
                .equals(PublishStatusEnum.PUBLISHED_STATUS.getResultCode()) || StringUtils.isEmpty(productId)) {
            logger.info("传入id为非启用状态或为空");
            throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        /* 查询数据 */
        ProductMarketingDiscourseDetailBO productMarketingDiscourseDetailBO = new ProductMarketingDiscourseDetailBO();
        BeanUtil.copyProperties(productMapper.selectById(productId), productMarketingDiscourseDetailBO);
        return productMarketingDiscourseDetailBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductCommissionRateList(java.lang.Integer)
     */
    @Override
    public IPage<ProductCommissionRateBO> getProductCommissionRateList(PageParam pageParam) {
        /* 查询数据 */
        Page<ProductCommissionRateBO> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        return productMapper.getProductCommissionRateList(page);
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#getProductOnApp
     * (com.newtouch.bxzs.business.product.model.ProductAppVO)
     */
    @Override
    public IPage<ProductOnAppBO> getProductListOnApp(ProductAppVO productAppVO) {
        /* 校验 */
        if (productAppVO == null || productAppVO.getCurrentPage() == null || productAppVO.getPageSize() == null || productAppVO.getCurrentPage() < 1) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        /* 查询数据 */
        Page<ProductOnAppBO> page = new Page<>(productAppVO.getCurrentPage(), productAppVO.getPageSize());
        return productMapper.getProductOnApp(page, productAppVO);
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductListByCompany(com.newtouch.bxzs.business.product.model.
     * ProductAppVO)
     */
    @Override
    public IPage<ProductOnAppBO> getProductListByCompany(ProductCompanyVO productCompanyVO) {
        /* 校验 */
        if (productCompanyVO == null || productCompanyVO.getCurrentPage() == null
                || productCompanyVO.getCurrentPage() < 1 ) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        /* 查询数据 */
        Page<ProductOnAppBO> page = new Page<>(productCompanyVO.getCurrentPage(), productCompanyVO.getPageSize());
        return productMapper.getProductListByCompany(page, productCompanyVO);
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductListByMarketing(java.lang.String)
     */
    @Override
    public ProductListMarketingBO getProductListByMarketing(String productName) {
        /* 查询产品集合 */
        List<ProductMarketingBO> productMarketingBOList = productMapper.getProductListByMarketing(productName);
        /* 实例化一个ProductListMarketingBO */
        ProductListMarketingBO productListMarketingBO = new ProductListMarketingBO();
        productListMarketingBO.setTotal(productMarketingBOList.size());
        /* 包装结构 */
        Map<String, List<ProductMarketingBO>> hashMap = new LinkedHashMap<>();
        for (ProductMarketingBO productMarketingBO : productMarketingBOList) {
            /* map中存在此id，将数据存放当前key的map中 */
            if (hashMap.containsKey(productMarketingBO.getProductNameFullSpelling().substring(0, 1).toUpperCase())) {
                hashMap.get(productMarketingBO.getProductNameFullSpelling().substring(0, 1).toUpperCase())
                        .add(productMarketingBO);
            } else {// map中不存在，新建key，用来存放数据
                List<ProductMarketingBO> productMarketingBOArrayList = new ArrayList<>();
                productMarketingBOArrayList.add(productMarketingBO);
                hashMap.put(productMarketingBO.getProductNameFullSpelling().substring(0, 1).toUpperCase(),
                        productMarketingBOArrayList);
            }
        }
        productListMarketingBO.setProductMarketingBOList(hashMap);
        return productListMarketingBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getProductRegionRanking(com.newtouch.bxzs.business.product.model.
     * ProductRegionRankingVO
     */
    @Override
    public ProductRegionRankingListBO getProductRegionRanking(ProductRegionRankingVO productRegionRankingVO) {
        //查看产品地区排名
        //效验入参
        productRegionRankingCheck(productRegionRankingVO.getStartTime(), productRegionRankingVO.getEndTime(), productRegionRankingVO.getRegionCode());
        //获取查看排行的数量
        Integer rankingNumber = Integer.parseInt(announcementService.getParameterValueRedis(SystemParamsEnum.PRODUCT_REGION_RANKING_NUMBER.getCode()));
        //查看排名列表
        List<ProductRegionRankingBO> list = productMapper.getProductRegionRankingList(productRegionRankingVO, rankingNumber);
        ProductRegionRankingListBO productRegionRankingListBO = new ProductRegionRankingListBO();
        //设置产品地区排名界面数据
        //设置开始年月和截止年月
        BeanUtil.copyProperties(productRegionRankingVO, productRegionRankingListBO);
        //如果没有选择产品，则不查看当前产品的排行
        if (productRegionRankingVO.getProductCode() != null) {
            //查看选择的产品的排行名称
            ProductRegionRankingBO bo = productMapper.getProductRegionRanking(productRegionRankingVO, productRegionRankingVO.getProductCode());
            BeanUtil.copyProperties(bo, productRegionRankingListBO);
        }
        //设置排名集合
        productRegionRankingListBO.setList(list);
        //设置配置的排名总数
        productRegionRankingListBO.setRankingNumber(rankingNumber);
        //设置地区名称
        productRegionRankingListBO.setRegionName(productMapper.getRegionName(productRegionRankingVO.getRegionCode()));
        return productRegionRankingListBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.ProductService#
     * getRankingProductList(com.newtouch.bxzs.business.product.model.
     * RankingProductListVO
     */
    @Override
    public RankingProductListBO getProductRankingList(RankingProductListVO rankingProductListVO) {
        //查看产品地区排名
        //效验入参
        if (StringUtils.isEmpty(rankingProductListVO.getRegionCode())) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR.getResultCode(), BaseErrorEnum.REQUEST_PARAMETER_ERROR.getResultMsg() + "地区编码不能为空");
        }
        //查询产品列表
        List<RankingProductBO> list = productMapper.getProductRankingList(rankingProductListVO.getProductType(),
                rankingProductListVO.getRegionCode(), rankingProductListVO.getDataSource());
        RankingProductListBO rankingProductListBO = new RankingProductListBO();
        rankingProductListBO.setList(list);
        rankingProductListBO.setTotal(list.size());
        return rankingProductListBO;
    }

    /**
     * 效验产品地区排名入参
     *
     * @param startTime  开始年月
     * @param endTime    截止年月
     * @param regionCode 地区编码
     */
    public void productRegionRankingCheck(Date startTime, Date endTime, String regionCode) {
        Assert.isTrue(!BeanUtil.isEmpty(startTime), "-1", "开始年月不能为空");
        Assert.isTrue(!BeanUtil.isEmpty(endTime), "-1", "结束年月不能为空");
        Assert.isTrue(!StringUtils.isEmpty(regionCode), "-1", "所在地区不能为空");
        //开始年月与结束年月相差不能大于12个月
        //效验参数365天
        int day = 365;
        if (endTime.after(DateUtil.offsetDay(startTime, day))) {
            logger.info("开始年月与结束年月相差不能大于12个月");
            throw new BaseException(BaseErrorEnum.BODY_NOT_MATCH.getResultCode(), "开始年月与结束年月相差不能大于12个月");
        }
        //结束年月必须不小于开始年月，且结束年月不得大于当前年月
        if (endTime.before(startTime)) {
            logger.info("结束年月必须不小于开始年月");
            throw new BaseException(BaseErrorEnum.BODY_NOT_MATCH.getResultCode(), "结束年月必须不小于开始年月");
        }
        if (endTime.after(new Date())) {
            logger.info("结束年月不得大于当前年月");
            throw new BaseException(BaseErrorEnum.BODY_NOT_MATCH.getResultCode(), "结束年月不得大于当前年月");
        }
    }
}
